using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

using System.Windows.Forms;

using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.WinForms;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using SkiaSharp;
using System.Reflection.Emit;

namespace JDTChart
{
    public partial class PassChartControl : UserControl
    {
        public ISeries[] Series { get; set; }
        private ColumnSeries<int> m_input_columnSeries {  get; set; }
        private ColumnSeries<int> m_onepass_columnSeries {  get; set; }
        private LineSeries<double> m_yieid_line {  get; set; }


        public PassChartControl()
        {
            InitializeComponent();
            Init();
        }

        public void update(string jsonStr)
        {
            try
            {
                var processor = DataProcessor.FromJson(jsonStr);
                m_input_columnSeries.Values = null;
                m_onepass_columnSeries.Values = null;
                m_yieid_line.Values = null;

                m_input_columnSeries.Values = processor.Input;
                m_yieid_line.Values = processor.Yieid;
                m_onepass_columnSeries.Values = processor.OnePass;

                var axisX = cartesianChart1.XAxes.FirstOrDefault();
                if (axisX != null)
                {
                    axisX.Labels = processor.Time;
                    axisX.LabelsRotation = 0; // 设置标签旋转角度
                }
                cartesianChart1.Update();
            }
            catch { }
        }
        private void Init()
        {
            try
            {
                m_input_columnSeries = new ColumnSeries<int>()
                {
                    Name = "input",
                    Values = [1, 2, 3]
                };
                m_onepass_columnSeries = new ColumnSeries<int>()
                {
                    Name = "一次PASS",
                    Values = []
                };
                m_yieid_line = new LineSeries<double>()
                {
                    Name = "直通率(%)",
                    Values = []
                };
                Series = [m_input_columnSeries, m_onepass_columnSeries, m_yieid_line];

                List<string> labels = new List<string>();

                cartesianChart1.Series = Series;
                cartesianChart1.XAxes = [new Axis{

                    TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    TicksAtCenter = true,  // 是否位于中间
                    MinStep = 1,
                    //ForceStepToMin = true,
                    SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                    Labels = labels,  // 你的标签数组
                }];
                cartesianChart1.YAxes = [new Axis{
                    TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    TicksAtCenter = true,  // 是否位于中间
                    SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                    Position = LiveChartsCore.Measure.AxisPosition.End
                },
                new Axis{
                    TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    TicksAtCenter = true,  // 是否位于中间
                    SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                    Position = LiveChartsCore.Measure.AxisPosition.Start
                }];
                cartesianChart1.LegendPosition = LiveChartsCore.Measure.LegendPosition.Bottom;
                cartesianChart1.LegendTextSize = 18;
                cartesianChart1.Dock = DockStyle.Fill;
            }
            catch (Exception ex)
            { }
        }
    }

    public class DataProcessor
    {
        public List<int> Input { get; set; }
        public List<int> OnePass { get; set; }
        public List<double> Yieid { get; set; }
        public List<string> Time { get; set; }

        public DataProcessor(List<int> input, List<int> onePass, List<double> yieid, List<string> time)
        {
            Input = input;
            OnePass = onePass;
            Yieid = yieid;
            Time = time;
        }

        public void Display()
        {
            Console.WriteLine("Input: " + string.Join(", ", Input));
            Console.WriteLine("OnePass: " + string.Join(", ", OnePass));
            Console.WriteLine("Yieid: " + string.Join(", ", Yieid));
            Console.WriteLine("Time: " + string.Join(", ", Time));
        }

        public static DataProcessor FromJson(string jsonStr)
        {
            var data = JObject.Parse(jsonStr);

            var input = data["Input"].ToObject<List<int>>();
            var onePass = data["OnePass"].ToObject<List<int>>();
            var yieid = data["Yieid"].ToObject<List<double>>();
            var time = data["Time"].ToObject<List<string>>();

            return new DataProcessor(input, onePass, yieid, time);
        }
    }
}
